package com.zou.application.controller.customer;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.zou.application.dao.ProductEntityMapper;
import com.zou.application.dao.ShopCarEntityMapper;
import com.zou.application.dao.UserEntityMapper;
import com.zou.application.entity.ProductEntity;
import com.zou.application.entity.ShopCarEntity;
import com.zou.application.entity.UserEntity;
import com.zou.application.utils.StringUtils;

@Controller
@RequestMapping("/shopcar")
public class ShopCarController {
	
	@Autowired
	private ShopCarEntityMapper shopCarDao;
	@Autowired
	private UserEntityMapper userDao;
	@Autowired
	private ProductEntityMapper productDao;
	
	@RequestMapping("/addCar")
	@ResponseBody
	public JSONObject addCar(HttpServletRequest request,HttpSession session){
		JSONObject result = new JSONObject();
		Integer userId = (Integer) session.getAttribute("userId");
		if(userId == null){
			result.put("msg", "请先登陆");
			result.put("code", "301");
			return result;
		}
		try {
			UserEntity userEntity = userDao.selectById(userId);
			String productId = request.getParameter("productId");
			ShopCarEntity shopCar = shopCarDao.selectProId(Integer.parseInt(productId));
			if(shopCar!=null){
				shopCar.setProductNum(shopCar.getProductNum()+1);
				shopCar.setModifyTime(new Date());
				shopCarDao.update(shopCar);
			}else{
				ProductEntity productEntity = productDao.selectById(Integer.valueOf(productId));
				ShopCarEntity shopCarEntity = new ShopCarEntity();
				shopCarEntity.setUserId(userId);
				shopCarEntity.setProductId(Integer.valueOf(productId));
				shopCarEntity.setProductName(productEntity.getName());
				shopCarEntity.setProductImg(productEntity.getImg());
				shopCarEntity.setCreateTime(new Date());
				shopCarEntity.setProductNum(1);
				shopCarEntity.setCreater(userEntity.getName());
				shopCarEntity.setSkuName(productEntity.getSku());
				shopCarEntity.setRelPrice(productEntity.getRelPrice());
				shopCarEntity.setDiscountPrice(productEntity.getDiscountPrice());
				shopCarEntity.setCountProce(productEntity.getRelPrice().subtract(productEntity.getDiscountPrice()));
				shopCarEntity.setReceiveAddress(userEntity.getAddress());
				shopCarEntity.setStatus((short)1);
				shopCarEntity.setIsDelete((short)0);
				shopCarDao.insert(shopCarEntity);
			}
			result.put("msg", "加如购物车成功");
			result.put("code", "200");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("msg", "系统异常");
			result.put("code", "500");
		}
		return result;
	}
	
	/**
	 * 购物车列表
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/shopcarList")
	public ModelAndView shopcarList(HttpServletRequest request,HttpSession session){
		ModelAndView mv = new ModelAndView();
		Integer userId = (Integer) session.getAttribute("userId");
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("userId", userId);
			param.put("status", 1);
			List<ShopCarEntity> shopCarEntities = shopCarDao.listByMap(param);
			mv.addObject("shopCars",shopCarEntities);
			mv.setViewName("/customer/shop_car");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	
	/**
	 * 删除购物车信息
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/deleShopCar")
	@ResponseBody
	public JSONObject deleShopCar(HttpServletRequest request,HttpSession session){
		JSONObject result = new JSONObject();
		try {
			Integer userId = (Integer) session.getAttribute("userId");
			String carId = request.getParameter("carId");
			UserEntity userEntity = userDao.selectById(userId);
			if(userEntity != null){
				shopCarDao.delete(Integer.valueOf(carId));
			}
			result.put("msg", "删除购物车成功");
			result.put("code", "200");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("msg", "系统异常");
			result.put("code", "500");
		}
		return result;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public JSONObject delete(HttpServletRequest request){
		JSONObject result = new JSONObject();
		String carId = request.getParameter("carId");
		if(StringUtils.isEmpty(carId)){
			result.put("code","100");
			result.put("msg","");
			return result;
		}
		try {
			shopCarDao.delete(Integer.parseInt(carId));
			result.put("code","200");
			result.put("msg","删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code","100");
			result.put("msg","系统异常");
		}
		return result;
	}
	
	@RequestMapping("/updateProNum")
	@ResponseBody
	public JSONObject updateProNum(HttpServletRequest request){
		JSONObject result = new JSONObject();
		String carId = request.getParameter("carId");
		String number = request.getParameter("number");
		
		if(StringUtils.isEmpty(carId)){
			result.put("code","100");
			result.put("msg","");
			return result;
		}
		try {
			
			ShopCarEntity shopCarEntity = shopCarDao.selectById(Integer.parseInt(carId));
			ProductEntity productEntity = productDao.selectById(shopCarEntity.getProductId());
			if(productEntity.getProductNum() < Integer.parseInt(number)){
				result.put("code","100");
				result.put("msg","库存不足");
				return result;
			}
			shopCarEntity.setProductNum(Integer.parseInt(number));
			shopCarEntity.setId(Integer.parseInt(carId));
			shopCarDao.update(shopCarEntity);
			result.put("code","200");
			result.put("msg","删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code","100");
			result.put("msg","系统异常");
		}
		return result;
	}
}
